import {arrayToObject} from '../../../../assets/js/util.js' 
export default{
	    data() {
	      return{
				title:'实时货币流水',
				xlsxTitle:'',
				loading:false,
				pickerOptions: {
	          	shortcuts: [{
		            text: '最近一周',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
		              picker.$emit('pick', [start, end]);
		            }
		          }, {
		            text: '最近15天',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
		              picker.$emit('pick', [start, end]);
		            }
		          }, {
		            text: '最近一个月',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
		              picker.$emit('pick', [start, end]);
		            }
		          }]
		        },
				dialogFormVisible: false,
				// 默认参数 和 URL
				SRC: '',
				DP: {
					p: '',
					f: ''
				},
			  	form: {
				  	date:'',
				  	dateStart: '',
				  	dateEnd:'',
					userid:'',
					roleid:'',
					platform:'0',
					platforms:[],
					channel:'0',
					channels:[],
					serverid:'0',
					serverids:[],
					currencytype:'',
					currencytypes:[],
					reason:'', 
					reasons:[], 

			  	},
				inputWidth:'220px',
				formLabelWidth: '120px',
		      	/*当前页显示数量*/
		      	pageSize:10,
		      	/*每页显示的数量*/
		      	pageSizes:[10, 20, 50, 100],
		      	/*dataTotal表数量*/
		      	dataTotal:0,
		      	/*表格数据*/
		      	realTimeServiceTableData:[],
		      	realTimeServiceTableDatas:[],
		      	/*realTimeServiceContent 的宽*/
		      	realTimeServiceContent:'100%',
		      	/*表的宽度*/
		      	realTimeServiceTableWidth: "100%",
		      	/*数据表头*/
		      	realTimeServiceTableHead:[
						{prop:"time",label:"时间",width:'200'},
						{prop:"plat",label:"平台",width:'120'},
						{prop:"channel",label:"渠道",width:'160'},
						{prop:"serviceArea",label:"区服",width:'160'},
						{prop:"accountId",label:"账号ID",width:'120'},
						{prop:"roleId",label:"角色ID",width:'120'},
						{prop:"roleGrade",label:"角色等级",width:'120'},
						{prop:"currencyType",label:"货币类型",width:'140'},
						{prop:"reason",label:"消耗/获得原因",width:'160'},
						{prop:"add_consume",label:"增加/消耗量",width:'200'},
						{prop:"balance",label:"消耗后余额",width:'180'}
	      	],
	        /*分页器*/
	        currentPage: 1,
	        rules: {
				    platform: [
				          { required: true, message: '请选择平台', trigger: 'change' }
				        ],
				    channel: [
				          { required: true, message: '请选择渠道', trigger: 'change' }
				        ],
				    serverid: [
				          { required: true, message: '请选择区服', trigger: 'change' }
				        ],
				    currencytype: [
				          { required: true, message: '请选择货币类型', trigger: 'change' }
				        ],
				    reason: [
				          { required: true, message: '请选择原因', trigger: 'change' }
				        ]
					}
		      }
		    },
			computed:{
				// xlsx(){
				// 	return this.title+'.xlsx'
				// }
			},
		methods: {
			arrayToObject(head,arr){  //数据与表头数量不同，特殊处理
				var arrNew=[];
				for(var i=0; i<arr.length; i++){
					var obj = {};
					for(var j in arr[i]){
			            if(j<=8){
			            	obj[head[j]['prop']] = arr[i][j]
			            }
			            else{
			            	if(j==10){
								obj[head[9]['prop']] = arr[i][10];
				            }else if(j==11){
				            	obj[head[10]['prop']] = arr[i][11];
				            }
			            } 
					}
					arrNew.push(obj)
				}
				return arrNew
			},
			successFun(data) {
		      this.$message({
		        message: data,
		        type: 'success'
		      });
		    },
	    	errorFun(data) {
		      this.$message.error(data);
		    },
			dateChange(val){
	    		var t = val.split(' - ')
	    		this.form.dateStart = t[0];
	    		this.form.dateEnd = t[1];
	    	},
	    	loads(){
	    		this.loading=!this.loading;	//加载中
				this.searchable = !this.searchable;//查询按钮不可用
	    	},
	    	// 平台切换 渠道切换 区服切换
			selectPlats(val) {
				this.reGetData(`updatePlatform.do?platformid=${val}`)
			},
			selectChannels(val) {
				this.reGetData(`updateChannel.do?channelid=${val}`)
			},
			selectServers(val) {
				this.reGetData(`updateServer.do?serverid=${val}`)
			},
	    	search(form){
				this.$refs[form].validate((valid) => {
		        if (valid) {
		         	this.commitData()
		        } else {
		          this.errorFun('请完善输入的值！');
		          return false;
		        }
		      })
			},
			commitData(){
				let obj={
					dateStart:this.form.dateStart,
					dateEnd:this.form.dateEnd,
					userid:this.form.userid,
					roleid:this.form.roleid,
					platform:this.form.platform,
					channel:this.form.channel,
					serverid:this.form.serverid,
					currencytype:this.form.currencytype,
					reason:this.form.reason
				}

				obj = encodeURI(JSON.stringify(obj));
				this.loads(); //加载 查询按钮不可用
				this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`,{
					params:{'ps':'params='+obj},
				}).then(function(res){
					this.loads();
	    			var data =  JSON.parse(JSON.parse(res.body))
	    			if(parseInt(data.res) === 0){
	  					if(data.data.length==0){
	    					this.errorFun('查询结果为空！')
	    				}else{
	  						this.realTimeServiceTableDatas =this.arrayToObject(this.realTimeServiceTableHead,data.data);
	  					}
		  			}else{
		  				this.errorFun(data["msg"])
		  			}
		  			this.dataTotal = this.realTimeServiceTableDatas.length;
	    			this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
	    		})
			},
			//重置
		    reset(form) {
		      this.$refs[form].resetFields();
		    },
			/*分页器*/
			handleSizeChange(val) {
				this.pageSize = val;
				// this.getData(this.pageSize,this.currentPage)
				this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				// this.getData(this.pageSize,this.currentPage)
				this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
			},
	    	/*获取数据*/
	    	getData: function(){
	    		this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`,{
					params:{'ps': '' }
					}).then(function(res){
	    			var data =  JSON.parse(JSON.parse(res.body))
	    			if(data.res == 0){
	    				this._dataPush(data.channelItmes,this.form.channels)
		    			this._dataPush(data.serverItmes,this.form.serverids)
		    			this._dataPush(data.platformItmes,this.form.platforms)
		    			this._dataPush(data.typeItmes,this.form.currencytypes)
		    			this._dataPush(data.reasonItmes,this.form.reasons)
	    			}else{
	    				this.getErrorMsg(Number(data.res))
	    			}
	    			
	    		})
	    	},
	    	/*获取数据*/
    	reGetData: function(url){
    		this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=${url}`,{
				params:{'ps': '' }
			}).then(function(res){
    			var data =  JSON.parse(JSON.parse(res.body))
    			if(data.res == 0){
    				if(url.indexOf('serverid')!=-1){
    					this.form.channels=[]
    					this.form.platforms=[]
	    				this._dataPush(data.channelItmes,this.form.channels)
		    			this._dataPush(data.platformItmes,this.form.platforms)
    				}else if(url.indexOf('platformid')!=-1){
    					this.form.channels=[]
    					this.form.serverids=[]
    					this._dataPush(data.channelItmes,this.form.channels)
	    				this._dataPush(data.serverItmes,this.form.serverids)
    				}else if(url.indexOf('channelid')!=-1){
    					this.form.serverids=[]
    					this.form.platforms=[]
		    			this._dataPush(data.serverItmes,this.form.serverids)
		    			this._dataPush(data.platformItmes,this.form.platforms)
    				}
    			}else{
    				this.errorFun(data.msg)
    			}
    			// this.form.channel = {label:this.form.channels[0]['label'],value:this.form.channels[0]['label']}
    		})
    	},
	    	_dataPush(firstData,lastData){
				for (var key in firstData){
			        lastData.push({label:key,value:firstData[key]})
				}
	    	},
	    	/*this.SRC+'moneyFlowNewAction/changeReason.do',{
					params:{typeItem:val},
				}*/
			currencytypeChange(val){ //根据货币类型 获取 消耗/获得原因
				this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=changeReason`,{
					params:{'ps':encodeURI(`typeItem=${val}`)},
				}).then(function(res){
	    			var data =  JSON.parse(JSON.parse(res.body))
	    			if(parseInt(data.res) === 0){
	    				this.form.reason='';
	    				this.form.reasons = [];
		    			this._dataPush(data.reasonItmes,this.form.reasons)
	    			}else{
	    				this.errorFun(data.msg)
	    			}
	    			
	    		})
			},
	    	/*设置初始值*/
	    	_init(){
	    		window.addEventListener('resize', function() {
	    			/*修改表格的宽度*/
	          		this.realTimeServiceTableWidth = document.body.clientWidth-240+"px";
	          		this.realTimeServiceContent = document.body.clientWidth-220+"px";
	        	}.bind(this))
	    	},
	    	// 表格下载
		  	excel(val) {
				this.xlsxTitle = val+"-"+this.form.dateStart+"到"+this.form.dateEnd;
				this.downloadExl(this.realTimeServiceTableHead,this.realTimeServiceTableDatas,this.xlsxTitle,this.title);
		  	}
	    },
	    mounted() {
	    	// 初始化URL
			this.SRC = this.$store.getters.src;
			this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
			this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
	     	this._init()
	     	this.getData()
	     	this.form.date = [new Date(new Date().getTime() - 86400000),new Date()];
	     	// this.getData(this.pageSize,this.currentPage)
	    }
	}